﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LDAPOfficeTool
{
    public class StringUtils
    {
        // New generation of transformations
        // transformation of one logical unit (string table) to one string; string table is just for storage and no other meaning
        
        public static string StringStorage2String (string[] String2Convert)
        {
            string RetVal;
            RetVal = "";
            if (String2Convert != null)
            {
                for (int i = 0; i < String2Convert.Length; i++)
                {
                    RetVal = RetVal + String2Convert[i];
                }
            }
            return RetVal;
        }


        public static string StringStorage2StringWithDelimiter(string[] String2Convert, string Delimiter)
        {
            string RetVal;
            RetVal = String2Convert[0];
            for (int i = 1; i < String2Convert.Length; i++)
            {
                RetVal = RetVal + Delimiter + String2Convert[i];
            }
            return RetVal;
        }


        // group for transformations for display
        public static string CutOffWithTail(string String2Cut, string StringTail, int MaxLen)
        {
            string sRetVal;
            sRetVal = "";
            if (String2Cut.Length > MaxLen)
            {
                sRetVal = String2Cut.Substring(0, MaxLen) + StringTail;
            }
            else
            {
                sRetVal = String2Cut;
            }
            return sRetVal;
        }


/*
        public static string StringTable2String(string[] st2Convert)
        {
            string sRetVal = st2Convert[0];
            for (int i = 1; i < st2Convert.Length; i++)
            {
                sRetVal = Environment.NewLine + sRetVal + i.ToString() + ": " + st2Convert[i];
            }
            return sRetVal;
        }
 */ 



        public static string StringTable2OneLine(string[] Arr2Convert, char Delimiter)
        {
            string RetVal;
            RetVal = Arr2Convert[0];

            for (int i = 1; i < Arr2Convert.Length; i++)
            {
                RetVal = RetVal + Delimiter + Arr2Convert[i];
            }
            return RetVal;
        }


        public static string StringTable2StringWithLine(string[] st2Convert)
        {
            string RetVal = "0: " + st2Convert[0];
            for (int i = 1; i < st2Convert.Length; i++)
            {
                RetVal = Environment.NewLine + i.ToString() + ": " + RetVal + i.ToString() + ": " + st2Convert[i];
            }
            return RetVal;
        }


        public static string StringTable2StringWLNeutral(string[] stConvert, string psDefaultRepresentation)
        {
            string RetVal;
            if (stConvert != null)
            {
                if (stConvert.Length > 1)
                {
                    RetVal = "0: " + stConvert[0];
                    for (int i = 1; i < stConvert.Length; i++)
                    {
                        RetVal = RetVal + Environment.NewLine + i.ToString() + ": " + stConvert[i];
                    }
                }
                else
                {
                    RetVal = stConvert[0];
                }
            }
            else
            {
                RetVal = psDefaultRepresentation;
            }
            return RetVal;
        }


        public static string String2MultilineString(string Str2Multiline, int LineLength)
        {
            string RetVal;
            RetVal = "";
            bool Done;
            Done = false;
            int Position = 0;
            while (!Done)
            {
                if (Position > Str2Multiline.Length)
                {
                    Done = true;
                }
                else
                {
                    if (Position + LineLength > Str2Multiline.Length)
                    {
                        RetVal = RetVal + Str2Multiline.Substring(Position);
                    }
                    else
                    {
                        RetVal = RetVal + Str2Multiline.Substring(Position, LineLength);
                        RetVal = RetVal + Environment.NewLine;
                    }
                    Position = Position + LineLength;
                }
            }
            return RetVal;
        }



        public static string FormatWholeWords(string Str2Format, int MaximalLength)
        {
            string RetVal;
            RetVal = "";
            string LineBeingProcessed;
            string[] PartArray;
            char[] SeparatorArray = { ' ' };
            PartArray = Str2Format.Split(SeparatorArray);
            LineBeingProcessed = PartArray[0];
            for (int i = 1; i < PartArray.Length; i++)
            {
                if (LineBeingProcessed.Length + PartArray[i].Length > MaximalLength)
                {
                    RetVal = RetVal + LineBeingProcessed + Environment.NewLine;
                    LineBeingProcessed = PartArray[i];
                }
                else
                {
                    LineBeingProcessed = LineBeingProcessed + " " + PartArray[i];
                }
            }
            RetVal = RetVal + LineBeingProcessed;
            return RetVal;
        }


        public static string Table2CommaCollection(string[] InputArray)
        {
            string RetVal;
            RetVal = "";
            if (InputArray[0] != null)
            {
                RetVal = InputArray[0];
            }
            for (int i = 1; i < InputArray.Length; i++)
            {
                RetVal = RetVal + "," + InputArray[i];
            }
            return RetVal;

        }
    }
}
